<?php


namespace app\api\controller\finance;


use app\api\controller\Api;
use app\common\order\model\OrderPay;
use util\Format;

class Payment extends Api
{
    public function index()
    {
        $post = $this->request->post();

        $where = [];
        $hasWhere = [];
        $hasWhere[] = ['refund_status', '=', 0];
        if (!empty($post['user_id'])) $hasWhere[] = ['user_id', 'in', $post['user_id']];
        if (!empty($post['company_id'])) $hasWhere[] = ['company_id', 'in', $post['company_id']];
        if (!empty($post['store_id'])) $hasWhere[] = ['store_id', 'in', $post['store_id']];
        if (!empty($post['category_id'])) $hasWhere[] = ['category_id', 'in', $post['category_id']];
        if (!empty($post['order_type'])) $hasWhere[] = ['order_type', 'in', $post['order_type']];
        if (!empty($post['check_status'])) $hasWhere[] = ['check_status', 'in', $post['check_status']];
        if (!empty($post['sign_date_range'])) {
            $signDateRange = Format::dateRange($post['sign_date_range']);
            $hasWhere[] = ['sign_date', 'between', $signDateRange];
        }
        if (!empty($post['event_date_range'])) {
            $eventDateRange = Format::dateRange($post['event_date_range']);
            $hasWhere[] = ['event_date', 'between', $eventDateRange];
        }
        if (!empty($post['keyword'])) $hasWhere[] = ['contact_no|bridegroom|bridegroom_mobile|bride|bride_mobile', 'like', "%{$post['keyword']}%"];

        list($cfg) = Format::pagination($post);
        $model = OrderPay::hasWhere('order', $hasWhere);
        $pg = $model->with('order')->where($where)->order('id desc')->paginate($cfg);
        $rows = $pg->getCollection();
        foreach ($rows as $row) {
            $row->append(['type', 'user', 'check_tag']);
            $row->order->append(['category', 'company', 'cooperation', 'user', 'store', 'hall', 'banquet_type', 'order_type_text', 'check_tag']);
        }

        $result = [];
        $result['list'] = $rows;
        $result['pagination'] = Format::pgResult($pg);

        return success($result);
    }
}